import React, { forwardRef, useImperativeHandle, useState } from 'react';

import { Button, Form, Input, Modal, Radio, Tag } from 'antd';
import { modalLayoutLg, modalLayoutSm } from '@/config/layout';
const AddM = (props: any, refs: any) => {
  const [form] = Form.useForm();
  // const [fileList,setfileList] = useState([]);
  const [measureClassData, setmeasureClassData] = useState([]);
  const { setFieldsValue, resetFields } = form;
  const [code, setCode] = useState('');
  const [isModalVisible, setIsModalVisible] = useState(false);

  //显示弹窗
  const showModal = () => {
    setIsModalVisible(true);
  };

  //取消
  const handleCancel = () => {
    setIsModalVisible(false);
    setCode('');
    resetFields();
    props.callback && props.callback();
  };
  //确定
  const handleOk = () => {
    form
      .validateFields()
      .then((vals) => {
        let Obj = form.getFieldsValue();
        console.log(Obj);
        Obj = {
          ...Obj,
          ...{
            topicPartitions: 1,
            topicReplication: 1,
          },
        };
        const fn = code ? topic.topicUpdate : topic.topicCreate;
        if (code) {
          Obj.topicId = code;
        } else {
          Obj.topicStat = 0;
        }
        fn(Obj).then((info: any) => {
          setIsModalVisible(false);
          handleCancel();
        });
      })
      .catch((info) => {});
  };

  const getDetail = (id: any) => {
    console.log(id);
  };
  useImperativeHandle(refs, () => ({
    init: (id: any, record: any) => {
      if (id) {
        setCode(id);
        console.log(record);
        setFieldsValue(record);
        getDetail(id);
      } else {
        resetFields();
      }
      showModal();
      // setfileList([]);
    },
  }));

  return (
    <>
      <Modal
        title={code === '' ? '新增' : '编辑'}
        visible={isModalVisible}
        onOk={handleOk}
        onCancel={handleCancel}
        centered
        width={700}
        maskClosable={false}
        bodyStyle={{
          maxHeight: window.innerHeight - 150,
          overflowY: 'auto',
        }}
      >
        <Form form={form} {...modalLayoutSm}>
          <Form.Item label="名称">
            <Input />
          </Form.Item>
        </Form>
      </Modal>
    </>
  );
};
const AddMCom = forwardRef(AddM);

export default AddMCom;
